var util = require('../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: "",
    time: "",
    diningList: [],
    typeId: 0,//餐桌类型
    typeName: "",
    phone:""
  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },

  bindTimeChange: function (e) {
    this.setData({
      time: e.detail.value
    })
  },
  
  callPhone:function(){
    var that = this;
    wx.makePhoneCall({
      phoneNumber: that.data.phone
    })
  },

  //获取店铺信息
  getStoreInfo: function () {
    var that = this;
    wx.request({
      url: app.globalData.host + "/api/Store/GetInfo?id=" + app.globalData.storeId,
      success: function (res) {
        if (res.data.Code == 0) {
          that.setData({
            phone: res.data.Data.Phone
          })
        }
      },
    })
  },

  //预约提交
  appointSubmit: function (e) {
    var that = this;
    let concatNumber = e.detail.value.concatNumber || ""//联系电话
    let concatPeople = e.detail.value.concatPeople || ""//联系人
    let diningTableUserTotal = e.detail.value.diningTableUserTotal//就餐人数
    let remark = e.detail.value.remark || ""//备注
    let date = that.data.date//日期
    let time = that.data.time//时间
    let typeId = that.data.typeId//餐位类型
    let applyTime = that.data.date + '\t' + that.data.time
    if (!date) {
      wx.showModal({
        title: '提示',
        content: '请选择日期',
        showCancel: false
      })
      return
    }
    if (!time) {
      wx.showModal({
        title: '提示',
        content: '请选择时间',
        showCancel: false
      })
      return
    }
    if (typeId == 0){
      wx.showModal({
        title: '提示',
        content: '请选择餐位类型',
        showCancel: false
      })
      return
    }
    if (diningTableUserTotal == 0) {
      wx.showModal({
        title: '提示',
        content: '请输入就餐人数',
        showCancel: false
      })
      return
    }
    if (!concatPeople) {
      wx.showModal({
        title: '提示',
        content: '请输入联系人',
        showCancel: false
      })
      return
    }
    if (!concatNumber) {
      wx.showModal({
        title: '提示',
        content: '请输入联系电话',
        showCancel: false
      })
      return
    }
    wx.request({
      url: app.globalData.host + '/api/user/AppointApply',
      method: 'POST',
      data: {
        Uid: app.globalData.userInfo.Uid,
        Pid: 0,
        ApplyTime: applyTime,
        RealName: concatPeople,
        Phone: concatNumber,
        Remark: remark,
        AppointType: 1,
        DiningTableTypeId: typeId,
        DiningTableUserTotal: diningTableUserTotal,
        DiningTableTypeName: that.data.typeName
      },
      success: function (res) {
        if (res.data.Code != 0) {
          wx.showToast({
            title: res.data.Message,
            icon: 'false',
            duration: 2000
          })
          return false;
        } else {
          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 2000
          })
          wx.redirectTo({
            url: '/pages/yuyue-list/index',
          })
        }
      }
    })
  },

  //获取餐桌类型设置
  getDiningTableType: function () {
    var that = this;
    wx.request({
      url: app.globalData.host + "/api/Restaurant/GetDiningTableType?storeId=" + app.globalData.storeId,
      success: function (res) {
        if (res.data.Code == 0) {
          that.setData({
            diningList: res.data.Data
          })
        }
      },
    })
  },

  selectItem: function (e) {
    var id = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    this.setData({
      typeId: id,
      typeName: name
    })
    this.hideModal()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getStoreInfo()
    this.getDiningTableType()
  },

  // 弹出层
  showModal: function (e) {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },

  hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  }
})